use proc_macro2::TokenStream as T2;
use quote::{format_ident, quote};
use syn::Ident;

use crate::periph::steal_periph;

pub enum RccAxB {
    AHB1,
    AHB2,
    AHB3,
    APB1,
    APB2,
}

pub fn rcc_enable_all(axb: RccAxB, fields: &[Ident], part: bool) -> T2 {
    let axb_enr = match axb {
        RccAxB::AHB1 => format_ident!("ahb1enr"),
        RccAxB::AHB2 => format_ident!("ahb2enr"),
        RccAxB::AHB3 => format_ident!("ahb3enr"),
        RccAxB::APB1 => format_ident!("apb1enr"),
        RccAxB::APB2 => format_ident!("apb2enr"),
    };
    let steal_rcc = steal_periph("RCC");
    let src = quote! {
        let rcc = #steal_rcc;
        rcc.#axb_enr().modify(|_, w| w #(.#fields().enabled())*);
    };

    if !part {
        src
    } else {
        quote! {
            {
                #src
            }
        }
    }
}

pub fn rcc_enable(axb: RccAxB, field: &Ident, part: bool) -> T2 {
    let axb_enr = match axb {
        RccAxB::AHB1 => format_ident!("ahb1enr"),
        RccAxB::AHB2 => format_ident!("ahb2enr"),
        RccAxB::AHB3 => format_ident!("ahb3enr"),
        RccAxB::APB1 => format_ident!("apb1enr"),
        RccAxB::APB2 => format_ident!("apb2enr"),
    };
    let steal_rcc = steal_periph("RCC");
    let src = quote! {
        let rcc = #steal_rcc;
        rcc.#axb_enr().modify(|_, w| w.#field().enabled());
    };
    if !part {
        src
    } else {
        quote! {
            {
                #src
            }
        }
    }
}
